﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using WWLauncher.Constant;
using WWLauncher.Interface;
using WWLauncher.Util;
using WWLauncher.ViewModel;

namespace WWLauncher.View
{
    /// <summary>
    /// PowerWindow.xaml
    /// </summary>
    public partial class PowerWindow : Window
    {
        readonly PowerWindowViewModel viewModel = new PowerWindowViewModel();
        public PowerWindow()
        {
            this.DataContext = viewModel;
            InitializeComponent();
            CreateAndHideWindow();

            Activated += (s, e) =>
            {
                if (ElementsList.ItemContainerGenerator.ContainerFromItem(ElementsList.SelectedItem) is FrameworkElement container)
                {
                    container.Focus();
                }
            };
            SourceInitialized += (s, e) => UpdateTheme();
            
        }
        private void CreateAndHideWindow()
        {
            // Ensure the Win32 and WPF windows are created to fix first show issues with DPI Scaling
            Opacity = 0;
            Show();
            Hide();
            Opacity = 1;
        }

        #region ShowingHiding
        public void ToggleWindowVisibility()
        {
            if (this.Visibility == Visibility.Visible)
            {
                this.HideWithAnimation();
            }
            else
            {
                viewModel.Refresh();
                UpdateTheme();
                UpdateWindowPosition();
                this.ShowWithAnimation();
            }
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
            this.HideWithAnimation();
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape || e.Key == Key.Enter)
            {
                this.HideWithAnimation();
            }
            else if (e.Key == Key.Down || e.Key == Key.Up)
            {
                HandleUpDownArrows(sender, e);
            }
        }

        private static void HandleUpDownArrows(object sender, KeyEventArgs e)
        {
            var list = (sender as PowerWindow)?.ElementsList;
            var focusedElement = (Keyboard.FocusedElement as UIElement);
            FocusNavigationDirection direction = FocusNavigationDirection.Next;

            if (e.Key == Key.Down) { direction = FocusNavigationDirection.Next; }
            else if (e.Key == Key.Up) { direction = FocusNavigationDirection.Previous; }

            if (focusedElement != null) focusedElement.MoveFocus(new TraversalRequest(direction));
            e.Handled = true;
        }
        #endregion

        #region ServicesUpdates
        private void UpdateTheme()
        {
            // Call UpdateTheme before UpdateWindowPosition in case sizes change with the theme.
            //ThemeService.UpdateThemeResources(Resources);
            //if (ThemeService.IsWindowTransparencyEnabled)
            //{
            //    this.EnableBlur();
            //}
            //else
            //{
            //    this.DisableBlur();
            //}
        }

        private void UpdateWindowPosition()
        {
            LayoutRoot.UpdateLayout();
            LayoutRoot.Measure(new Size(double.PositiveInfinity, MaxHeight));
            Height = LayoutRoot.DesiredSize.Height;

            var taskbarState = TaskbarService.GetWinTaskbarState();
            switch (taskbarState.TaskbarPosition)
            {
                case TaskbarPosition.Left:
                    Left = (taskbarState.TaskbarSize.Right / this.DpiWidthFactor());
                    Top = (taskbarState.TaskbarSize.Bottom / this.DpiHeightFactor()) - Height;
                    break;
                case TaskbarPosition.Right:
                    Left = (taskbarState.TaskbarSize.Left / this.DpiWidthFactor()) - Width;
                    Top = (taskbarState.TaskbarSize.Bottom / this.DpiHeightFactor()) - Height;
                    break;
                case TaskbarPosition.Top:
                    Left = (taskbarState.TaskbarSize.Right / this.DpiWidthFactor()) - Width;
                    Top = (taskbarState.TaskbarSize.Bottom / this.DpiHeightFactor());
                    break;
                case TaskbarPosition.Bottom:
                    Left = (taskbarState.TaskbarSize.Right / this.DpiWidthFactor()) - Width;
                    Top = (taskbarState.TaskbarSize.Top / this.DpiHeightFactor()) - Height;
                    break;
            }
        }
        #endregion

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.HideWithAnimation();
        }

        private void LayoutRoot_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollViewer scv = (ScrollViewer)sender;
            scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);
            e.Handled = true;
        }
    }
     
}
 